﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DungChung;
using System.Net;
using System.IO;
using System.Net.Sockets;
namespace CoVuaServer
{
    class QuanliKetNoi
    {
        private TaiKhoanDTO _taiKhoan;
        private bool dangRanh;
        public TaiKhoanDTO TaiKhoan
        {
            get { return _taiKhoan; }
            set { _taiKhoan = value; }
        }

        public TcpClient Clients
        {
            get { return _Clients; }
            set { _Clients = value; }
        }

        public bool DangRanh
        {
            get { return dangRanh; }
            set { dangRanh = value; }
        }

        public QuanliKetNoi NguoiCungChoi
        {
            get { return _nguoiCungChoi; }
            set { _nguoiCungChoi = value; }
        }

        public BinaryReader Reader
        {
            get { return reader; }
            set { reader = value; }
        }

        public BinaryWriter Writer
        {
            get { return writer; }
            set { writer = value; }
        }

        private NetworkStream stream;
        private BinaryReader reader;
        private BinaryWriter writer;
        private TcpClient _Clients;
        private QuanliKetNoi _nguoiCungChoi;
        public QuanliKetNoi(TcpClient client, TaiKhoanDTO taiKhoan)
        {
            Clients = client;
            TaiKhoan = taiKhoan;
            stream = Clients.GetStream();
            Writer = new BinaryWriter(stream);
            Reader = new BinaryReader(stream);
            DangRanh = true;
        }

        public QuanliKetNoi()
        {
            // TODO: Complete member initialization
        }
        public void SendInfo()
        {
            Writer.Write(TaiKhoan.TenHienThi);
            Writer.Write(TaiKhoan.Status);
            Writer.Write(TaiKhoan.KinhNghiem);
            Writer.Write(TaiKhoan.GioiThieu);
        }
        public void SendSoLuong(int soluong)
        {
            Writer.Write(soluong);
        }
        public void DeNghiNhanDanhSach()
        {
            Writer.Write("DanhSachNguoiChoi");
        }
        public void SendAnotherUser(TaiKhoanDTO taiKhoan)
        {
            Writer.Write(taiKhoan.UserName);
            Writer.Write(taiKhoan.TenHienThi);
            Writer.Write(taiKhoan.Status);
            Writer.Write(taiKhoan.KinhNghiem);
            Writer.Write(taiKhoan.GioiThieu);
            Writer.Write(DangRanh);
        }
        public void CoNguoiMoiDangNhap()
        {
            Writer.Write(MyTask.CoNguoiDangNhap);
        }
        public void CoNguoiMoiDangXuat()
        {
            Writer.Write(MyTask.CoNguoiDangXuat);
        }
        public string ReadRequest()
        {
            return Reader.ReadString();
        }
        public void DongKetNoi()
        {
            stream.Close();
            Clients.Close();
        }
        public void GuiLoiMoi(TaiKhoanDTO taiKhoan)
        {
            Writer.Write(MyTask.LoiMoiThamGiaVanDau);
            Writer.Write(taiKhoan.UserName);
            Writer.Write(taiKhoan.TenHienThi);
        }
        public void DaTuChoi()
        {
            Writer.Write(MyTask.TuChoi);
        }
        public void DaNhanLoi(string username)
        {
            Writer.Write(MyTask.NhanLoiMoi);
            Writer.Write(username);
        }
        public void NguoiChoiDangBan(string tenHienThi)
        {
            Writer.Write(MyTask.NguoiChoiDangBan);
            Writer.Write(tenHienThi);
        }
        public void CoNguoiTaoMoiTranDau(string user1, string user2)
        {
            Writer.Write(MyTask.CoNguoiTaoMoiTranDau);
            Writer.Write(user1);
            Writer.Write(user2);
        }
        public void SendMessageChat(string nguoiGui, string NoiDung)
        {
            Writer.Write(MyTask.Chat);
            Writer.Write(nguoiGui);
            Writer.Write(NoiDung);
        }
        public void NguoiChoiXinThua()
        {
            Writer.Write(MyTask.XinThua);
        }
        public void XuLyNuocDi()
        {
            if (NguoiCungChoi != null)
            {
                NguoiCungChoi.Writer.Write(MyTask.NuocDi);
                NguoiCungChoi.Writer.Write(Reader.ReadInt32());
                NguoiCungChoi.Writer.Write(Reader.ReadInt32());
                NguoiCungChoi.Writer.Write(Reader.ReadInt32());
                NguoiCungChoi.Writer.Write(Reader.ReadInt32());
                NguoiCungChoi.writer.Write(reader.ReadBoolean());
            }
            else
            {
                Reader.ReadInt32();
                Reader.ReadInt32();
                Reader.ReadInt32();
                Reader.ReadInt32();
                reader.ReadBoolean();
            }
            
        }
    }
}
